<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="openItemDialog(false)">添加分级</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="等级" prop="name" min-width="180" align="center"></el-table-column>
        <el-table-column label="广告费用分摊比例" prop="adPercent" min-width="180" align="center"></el-table-column>
        <el-table-column label="备注" prop="remark" min-width="180" align="center"></el-table-column>
        <el-table-column label="操作" min-width="180" align="center">
          <template slot-scope="scope">
            <el-button @click="openItemDialog(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="removeItem(scope.row.id)" type="text" size="small">删除</el-button>
            <el-button @click="openCountryDialog(scope.row.id)" type="text" size="small">查看国家</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="市场分级管理" :visible.sync="itemDialog" width="600px" :close-on-click-modal="false"> 
      <el-form :model="form" style="padding-right:100px;padding-left: 50px;" label-width="140px">
        <el-form-item label="等级名称：">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="广告费用分摊比例：">
          <el-input-number style="width: 100%;" v-model="form.adPercent" :min="0" :max="100"></el-input-number>
        </el-form-item>
         <el-form-item label="备注：">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="itemDialog = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="国家分级管理" :visible.sync="countryDialog" width="880px" :close-on-click-modal="false"> 
      <div>
        <el-form :inline="true">
          <el-form-item label="国家编码或名称">
            <el-input v-model="query"></el-input>
          </el-form-item>
          <el-form-item> 
            <el-button type="primary" @click="searchCustorm()">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table max-height="500" :data="countryData" border style="width: 100%">
          <el-table-column label="国家编码" prop="code" min-width="180" align="center"></el-table-column>
          <el-table-column label="国家名称" prop="name" min-width="180" align="center"></el-table-column>
          <el-table-column label="所属市场" prop="level" min-width="180" align="center">
            <template slot-scope="scope">
              <el-select v-model="scope.row.level" @change="saveChange(scope.row.level, scope.row.code)" placeholder="请选择">
                <el-option v-for="item in tableData" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="180" align="center">
            <template slot-scope="scope">
              <el-button @click="postChange(scope.row.code)" type="text" size="small">保存</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="countryDialog = false">取 消</el-button>
        <el-button type="primary" @click="countryDialog = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import dingMixin from  '@/mixins/ding'
  import formUtil from '@/utils/formUtil'

  export default {
    data () {
      return {
        itemDialog: false,
        kpiDialog: false,
        createLoading: false,
        kpiRatioDialog: false,
        show: true,
        countryDialog: false,
        countryData: [],
        month: '',
        form: {
          id: '',
          name: '',
          adPercent: '',
          remake: ''
        },
        formVerify: {
          name: '请输入等级名称'
        },
        tableData: [],
        countryDataCopy: [],
        changeDataCache: {},
        currentLevel: '',
        query: ''
      }
    },
    components:{

    },
    mixins: [dingMixin],
    mounted(){
      this.searchItem();
    },
    methods: {
      openItemDialog(item) {
        if(item) {
          let editData = formUtil.deepCopy(item);
          this.form = editData;
        }else{
          formUtil.resetData(this.form);
        }
        this.itemDialog = true;
      },
      removeItem(id) {
        formUtil.submitConfirm('此操作将永久删除团队, 是否继续?', async ()=> {
          if(!await this.$httpN.postB('mpb/market/deleteLevel', [id], false)) return;
          formUtil.submitSuccess('删除成功');
          this.searchItem()
        })
      },
      async searchItem() {
        this.tableData = await this.$httpN.get('mpb/market/listLevel')
      },
      async save() {
        if(!formUtil.verifyData(this.form, this.formVerify)) return;
        if(!await this.$httpN.postB('mpb/market/saveLevel', this.form)) return;
        this.itemDialog = false;
        formUtil.submitSuccess('保存成功');
        this.searchItem();
      },
      async openCountryDialog(id) {
        this.currentLevel = id;
        this.query = '';
        this.countryData = await this.$httpN.postQ('mpb/market/listCountry', {
          level: id,
          query: this.query
        })
        this.changeDataCache = {};
        this.countryDialog = true;
      },
      async searchCustorm() {
        this.countryData = await this.$httpN.postQ('mpb/market/listCountry', {
          level: this.currentLevel,
          query: this.query
        })
        this.changeDataCache = {};
      },
      saveChange(value, key) {
        this.changeDataCache[key] = value;
      },
      async postChange(key) {
        if(this.changeDataCache[key] === undefined) return this.$message.error('当前值未改变')
        if(!await this.$httpN.postQ('mpb/market/setCountryLevel', {
          countryCode: key,
          level: this.changeDataCache[key],
        },false)) return;
        formUtil.submitSuccess('变更成功');
        this.openCountryDialog(this.currentLevel)
      }
    }
  }
</script>
